<?php
	/**
	* Factory Connection
	*
	* 数据库链接工厂
	*/	
	class FactoryConnection
	{
		   public static function createConnect($type="Connect",$connectionConfigrationName="Default")
		   {
				switch (strtolower($type))
				{
					case "connect":
						$temp=new Connection_Connect($connectionConfigrationName);
						return $temp->createConnection();
					break;
					case "mysqli":
						$temp=new Connection_Mysqli($connectionConfigrationName);
						return $temp->createConnection();
					break;
					case "pdo":
						$temp=new Connection_PDO($connectionConfigrationName);
						return $temp->createConnection();
					break;
					default:
						printError($_SERVER['SCRIPT_NAME']." 创建Connection类发生错误","提供的数据库链接创建方式不存在。您提供的方式名是：".$type);
				}
		   }
		   
		   public static function closeConnection(&$source)
		   {
				if(isset($source)&&$source!=null)
				{
					switch (get_class($source))
					{
						case "mysqli":
							$source->Close();
							break;
						case "pdo":
							$source->Close();
							break;
						default:
							if(get_resource_type($source)=="mysql link"){
								mysql_close($source);
							}
					}
					$source=null;
				}                   
		   }
	}
	#数据库连接-抽象父类
	abstract class Connection
	{
		protected $dbname="";
		protected $host="";
		protected $user="";
		protected $password="";

		public function __construct($connectonStringName="Default")
		{
			global $Configuration;

			if(!array_key_exists($connectonStringName,$Configuration["ConnectionString"]))
			{
				printError($_SERVER['SCRIPT_NAME']." 创建Connection类发生错误","提供的数据库链接配置不存在。您提供的配置名是：".$connectonStringName);
			}

			$this->dbname=$Configuration["ConnectionString"]['Default']['DbName'];
			$this->user=$Configuration["ConnectionString"]['Default']['User'];
			$this->password=$Configuration["ConnectionString"]['Default']['Password'];
			$this->host=$Configuration["ConnectionString"]['Default']['Host'];
			$this->charset=$Configuration["ConnectionString"]['Default']['Charset'];
		}

		public function createConnection()
		{}
	}
	#数据库连接-使用mysql_connect进行连接
	class Connection_Connect extends Connection
	{           
		public function createConnection()
		{
				$link=mysql_connect($this->host,$this->user,$this->password);
				mysql_query("SET NAMES".$this->charset."");
				mysql_select_db($this->dbname);
				return $link;
		}
	}
	#数据库连接-使用mysqli进行连接
	class Connection_Mysqli extends Connection
	{
		public function createConnection()
		{
			 $mysqli=new mysqli($this->host,$this->user,$this->password,$this->dbname);
			 $mysqli->query("SET NAMES".$this->charset."");
			 return $mysqli;
		}
	}
	#数据库连接-使用pdo进行连接
	class Connection_PDO extends Connection
	{
		public function createConnection()
		{
			 $pdo=new PDO("mysql:host=localhost;dbname=test",$this->user,$this->password);
			 $pdo->query("SET NAMES".$this->charset."");
			 return $pdo;
		}
	}



/**
* Cedar Services API
* 提供调用"Cedar Service"的通用调用接口
*/	
class Service_Cedar
{
    #验证管理员是否登录
    public static function Manager_IsLogin()
    {
        $servicePassword=md5('HaLiLuYa');
        $serviceURL='http://localhost/services/Manager/Valid.php';
        $result=queryWebservice($serviceURL,'_servicePassword_='.$servicePassword,'POST',TRUE);
        return $result;
    }

    #管理员登录服务器
    public static function Manager_ToLogin($userid,$password,$isSetCookiesExpire)
    {
        $servicePassword=md5('HaLiLuYa');
        $serviceURL='http://localhost/services/Manager/Login.php';
        $result=queryWebservice($serviceURL,'_servicePassword_='.$servicePassword.'&userid='.$userid.'&password='.$password.'&isSetCookiesExpire='.$isSetCookiesExpire,'POST',TRUE);
        return $result;
    }

    #获取管理员密码
    public static function Manager_getPassword()
    {

    }

    #获取管理员权限
    public static function Manager_getRole()
    {

    }

    #变更管理员密码
    public static function Manager_editPassword()
    {

    }

    #变量管理员授权
    public static function Manager_editRoleAuthorization()
    {

    }

    #验证管理员模块权限
    public static function Manager_IsHaveAuthoriztion($authorizationNumber)
    {

    }
}
?>